package controller

import (
	"github.com/gin-gonic/gin"
	"mall/admin/web/models"
	"mall/admin/web/service"
	"mall/admin/utils"
)

type(
	UserController struct {
		*service.UserService
    }
	LoginRequest struct{
		Username string `json:"username" binding:"required"`
		Password string `json:"password" binding:"required"`
	}

	PageQuery struct{
		PageSize int `json:"pageSize" binding:"required"`
		Page int `json:"page" binding:"required"`
	}
	FoodResponse struct{
        List []models.Food `json:"list"`
        Total int64   `json:"total"`
	}
)

func NewUserController(s *service.UserService) *UserController  {

	return &UserController{s}

}

func (ctrl *UserController)Register(ctx *gin.Context)  {
    //var registerRequest RegisterRequest
	//
	//
	//if err:=ctx.ShouldBindJSON(&registerRequest);err!=nil{
	//	ctx.JSON(http.StatusBadRequest,gin.H{"error":err})
	//	return
	//}
	//
	//ctrl.InsertUser(registerRequest.Name,registerRequest.Age)
	//ctx.JSON(http.StatusOK,gin.H{"msg":registerRequest})

}

func (ctrl *UserController)Login(ctx *gin.Context)  {

     var loginRequest LoginRequest

     if err:= ctx.ShouldBindJSON(&loginRequest);err!=nil{
     	ctx.JSON(200,utils.Fail(err))
     	return
	 }
     //TODO 登錄邏輯判斷
     // ......
     //登錄成功生成token串
	token, err := utils.Sign(loginRequest.Username)
	if err!=nil{
		ctx.JSON(200,utils.Fail(err))
		return
	}
	successResponse:=struct{ Token string `json:"token"`}{
			Token:token,}
	ctx.JSON(200,utils.Success(successResponse))

}

func (ctrl *UserController)SetFood(ctx *gin.Context)  {


}

func (ctrl *UserController)FoodList(ctx *gin.Context)  {

	var pageQuery PageQuery

	if err:=ctx.ShouldBindJSON(&pageQuery);err!=nil{
		ctx.JSON(200,utils.Fail(err))
		return
	}
	total,foods, err := ctrl.FindAll(pageQuery.PageSize,pageQuery.Page)
	if err!=nil{
		ctx.JSON(200,utils.Fail(err))
		return
	}
	rsp:=FoodResponse{
		Total:total,
		List:foods,
	}
	ctx.JSON(200,utils.Success(rsp))
}

func (ctrl *UserController)Join(ctx *gin.Context)  {

     userGroup,err:=ctrl.FindBusiness()
     if err!=nil{

	 }
     ctx.JSON(200,utils.Success(userGroup))

}
